package com.qinglei.recoup.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qinglei.recoup.common.domain.CommonConstant;
import com.qinglei.recoup.common.domain.RadarData;
import com.qinglei.recoup.common.domain.RadarMessage;
import com.qinglei.recoup.common.service.RedisService;
import com.qinglei.recoup.common.utils.RecoupUtil;
import com.qinglei.recoup.system.dao.RadarDataMapper;
import com.qinglei.recoup.system.domain.Bed;
import com.qinglei.recoup.system.domain.User;
import com.qinglei.recoup.system.pojo.RealTimeDataVOV7;
import com.qinglei.recoup.system.pojo.RealTimeV7Cache;
import com.qinglei.recoup.system.service.BedService;
import com.qinglei.recoup.system.service.RadarDataService;
import com.qinglei.recoup.system.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author: 最爱学英语的cxd
 * @slogan: 一入代码深似海，从此红尘是路人
 * @create: 2022-03-09
 **/
@Slf4j
@Service
public class RadarDataServiceImpl extends ServiceImpl<RadarDataMapper, RadarData> implements RadarDataService {

    private static final String WEB_BREATH_OPERATION_V7 = "WEB:REALTIMEDATA:BREATH:V7:OPERATION:";
    private static final String WEB_BREATH_PHI_V7 = "WEB:REALTIMEDATA:BREATH:V7:PHI:";

    //V7Cache
    private static final String WEB_HEART_V7_REAL_TIME_CACHE = "WEB:HEART:V7:REAL:TIME:CACHE:";






    private static final double PI = 3.1416;
    //呼吸深度的阈值---------------------------
    private static final double BreathDeepth_th = 1.0;
    @Resource
    private RedisService redisService;
    @Resource
    private BedService bedService;

    //心跳----------------------------
    //100Hz绘制心跳波形
    private static final double HeartWaveFreq = 100;

    //实时展示心跳BPM最小值
    private static final Integer HeartBPM_min = 65;
    //实时展示心跳BPM最大值
    private static final Integer HeartBPM_Max = 80;

    private final static  //100Hz QRST波模型 固定
    String Buff_Electrocardiogram_QRST_String = "0,0,-2,-1,2,5,9,5,2,-4,-3,0,0.25,0.25,0.25,0.5,1,1.5,2,2.5,2.75,3,3,3,2.75,2.5,2,1.5,1,0.5,0.5,0.5,0.25,0.25,0.5,0.5,0.25,0,0,0,0,0,0,0";
    //固定
    private final static String Buff_Electrocardiogram_PR_String = "0,0,0.25,0.75,1.25,1.5,1.5,1.25,0.75,0.25,0,0,0,0,0,0,0,0,0,0,0,0";

    //100Hz QRST波模型
    String[] Buff_Electrocardiogram_QRST_S = Buff_Electrocardiogram_QRST_String.split(",");

    //100Hz PR波模型
    String[] Buff_Electrocardiogram_PR_S = Buff_Electrocardiogram_PR_String.split(",");


    //暂时------------------------------
    //呼吸BPM
    private final List<Integer> Record_BreathRateEstBPM = buildBreathBpm();
    //呼吸深度
    private final List<Double> Record_BreathDeepth = buildBreathDeepth();

    //心跳BPM
    private final List<Integer> Record_HeartRateBpm = buildHeartBpm();


    @Override
    public void saveRadarUserData(RadarMessage message) {
        RadarData radarData = new RadarData();
        radarData.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        radarData.setSn(message.getSn());
        radarData.setVersionInfo(message.getProCode());
        radarData.setJsonData(JSON.toJSONString(message.getData()));
        baseMapper.insert(radarData);
    }

    @Override
    public RealTimeDataVOV7 getRealTimeDataV7(String sn, Integer second) throws Exception {
        Bed bedBySn = bedService.getBedBySn(sn);
        RealTimeDataVOV7 realTimeDataVOV7 = this.detailV7Data(sn, second);
        realTimeDataVOV7.setIsOffline(bedBySn.getIsRadarOnline() == 1);
        realTimeDataVOV7.setIsSignal(bedBySn.getIsRadarNetwork() == 0);
        realTimeDataVOV7.setVersion(7);
        this.setStatus(bedBySn, realTimeDataVOV7);
        //如果设备标志存在，状态改为5
        String isSingularNum = CommonConstant.WEB_REAL_TIME_DATA_DEVICE_SINGULAR_IS + sn;
        if (redisService.exists(isSingularNum)) {
            realTimeDataVOV7.setStatus(5);
        }
        return realTimeDataVOV7;
    }

    @Override
    public void deleteV7RealTimeData(String sn) {
        if (StringUtils.isEmpty(sn)) {
            return;
        }
        try {
            User currentUser = RecoupUtil.getCurrentUser();
            String userIdAndSn = sn + ":" + currentUser.getId();
            redisService.del(WEB_BREATH_OPERATION_V7 + userIdAndSn);
            redisService.del(WEB_BREATH_PHI_V7 + userIdAndSn);
            redisService.del(WEB_HEART_V7_REAL_TIME_CACHE + userIdAndSn);
        } catch (Exception e) {
            log.error("RadarDataServiceImpl deleteV7RealTimeData {}", e);
        }

    }

    private void setStatus(Bed bed, RealTimeDataVOV7 realTimeDataVOV7) {
        if (bed.getIsRadarOnline() == Bed.RADAR_OFFLINE) {  //离线
            realTimeDataVOV7.setStatus(3);
        } else if (bed.getIsRadarOn() == Bed.RADAR_OFF) {  //关闭
            realTimeDataVOV7.setStatus(4);
        } else if (bed.getStatus() == Bed.STATUS_ALARM) {
            this.setStatusOutBed(bed, realTimeDataVOV7);
            realTimeDataVOV7.setOutBedAlarm(true);
        } else if (bed.getStatus() == Bed.STATUS_NORMAL) {
            realTimeDataVOV7.setOutBedAlarm(false);
            switch (bed.getSubStatus()) {
                case Bed.SUB_STATUS_OUT_BED:
                    this.setStatusOutBed(bed, realTimeDataVOV7);
                    break;
                case Bed.SUB_STATUS_CLEAR:
                    realTimeDataVOV7.setStatus(1);
                    realTimeDataVOV7.setSubStatus(1);
                    break;
                case Bed.SUB_STATUS_LIGHT_SLEEP:
                    realTimeDataVOV7.setStatus(1);
                    realTimeDataVOV7.setSubStatus(2);
                    break;
                case Bed.SUB_STATUS_DEEP_SLEEP:
                    realTimeDataVOV7.setStatus(1);
                    realTimeDataVOV7.setSubStatus(2);
                    break;
                default:
                    log.warn("setStatus 不支持的子状态 bed:{}", bed);
            }
        } else {
            log.warn("setStatus 不支持的状态 bed:{}", bed);
        }
    }

    /**
     * 设置离床状态
     *
     * @param bed
     * @param realTimeDataVOV7
     */
    private void setStatusOutBed(Bed bed, RealTimeDataVOV7 realTimeDataVOV7) {
        realTimeDataVOV7.setStatus(2);
        LocalDateTime happenTime = bed.getHappenTime();
        long duration = System.currentTimeMillis() / 1000 - happenTime.toEpochSecond(ZoneOffset.of("+8"));
        realTimeDataVOV7.setDuration((int) duration);
    }

    private RealTimeDataVOV7 detailV7Data(String sn, Integer second) throws Exception {
        RealTimeDataVOV7 realTimeDataVOV7 = new RealTimeDataVOV7();
        //查询second秒以前的数据，取1条
        List<RadarData> radarDataList = baseMapper.selectRadarDataData(sn, second);
        if (CollectionUtils.isEmpty(radarDataList)) {
            log.info("getRealTimeDataV7 数据不存在 sn{}，second{}", sn, second);
            return realTimeDataVOV7;
        }
        String jsonData = radarDataList.get(0).getJsonData();
        Map map = JSONUtil.jsonToMap(jsonData);
        Integer L2R = (Integer) map.get("L2R");
//        realTimeDataVOV7.setFrameNo();
        Integer TCF = (Integer) map.get("TCF");//目标过近标志；1=过近，0=正常；（最高位）
        Integer TSF = (Integer) map.get("TSF"); //目标平稳测结果标志；1=平稳，0=非平稳（最高位）
        Integer OBF = (Integer) map.get("OBF");//最终离床标志 1=离床，0=在床

        Integer breathBpm = (Integer) map.get("BBPM");//呼吸BPM
        Integer heartBpm = (Integer) map.get("HBPM");//心率BPM

        BigDecimal breathDeepth = (BigDecimal) map.get("BD");//呼吸深度（均值滤波后的结果）
        realTimeDataVOV7.setTCFg(TCF);
        realTimeDataVOV7.setTSFg(TSF);
        realTimeDataVOV7.setOBFg(OBF);
        log.info("detailV7Data ");
        //计算体动百分比
        Integer numL2r = 0;
        if (L2R != null) {
            int L2Rabs = Math.abs(L2R);
            if (L2Rabs > 1000) {
                L2Rabs = 1000;
            }
            numL2r = L2Rabs / 10;
        }
        int stableNum = 100 - numL2r;
        realTimeDataVOV7.setStableNum((double) stableNum);
        //如果是离床
        List<Double> breathPhaseList = new ArrayList<>();
        List<Double> heartList = new ArrayList<>();
        if (OBF == 1) {
            for (int i = 0; i < 10; i++) {
                breathPhaseList.add(0.0);
            }
            realTimeDataVOV7.setBreathPhaseData(breathPhaseList);
            for (int i = 0; i < 100; i++) {
                heartList.add(0.0);
            }
            realTimeDataVOV7.setHeartPhaseData(heartList);
            return realTimeDataVOV7;
        }

        breathPhaseList = breathV7DataBuild(sn, breathBpm, breathDeepth.doubleValue());
        realTimeDataVOV7.setBreathPhaseData(breathPhaseList);
        realTimeDataVOV7.setBreathBpm(breathBpm);
        //心跳----------------
        List<Double> Buff_Electrocardiogram_QRST = new ArrayList<>();
        for (int i = 0; i < Buff_Electrocardiogram_QRST_S.length; i++) {
            Buff_Electrocardiogram_QRST.add(Double.valueOf(Buff_Electrocardiogram_QRST_S[i]));
        }
        List<Double> Buff_Electrocardiogram_PR = new ArrayList<>();
        for (int i = 0; i < Buff_Electrocardiogram_PR_S.length; i++) {
            Buff_Electrocardiogram_PR.add(Double.valueOf(Buff_Electrocardiogram_PR_S[i]));
        }
        //QRSTPR波对应的时间（毫秒）
        int HeartWave_QRSTPR_cnt = Buff_Electrocardiogram_QRST.size() + Buff_Electrocardiogram_PR.size();
        //心跳波形模型初始化
        List<Double> HeartWave_Y_Mode = new ArrayList<>(200);
        for (int i = 0; i < 200; i++) {
            HeartWave_Y_Mode.add(0.0);
        }
        heartList = heartV7DataBuild(sn, heartBpm, HeartWave_QRSTPR_cnt, HeartWave_Y_Mode, Buff_Electrocardiogram_QRST, Buff_Electrocardiogram_PR);
        realTimeDataVOV7.setHeartPhaseData(heartList);
        realTimeDataVOV7.setHeartBpm(heartBpm);
        return realTimeDataVOV7;
    }

    @Override
    public void test() {

        List<Integer> HeartBpmList = buildHeartBpm();
        System.out.println("输入数据为：" + HeartBpmList);
        List<Double> saveDate = new ArrayList<>();

        for (int i = 0; i < HeartBpmList.size(); i++) {
            try {
                System.out.println("开始第"+i+"个数，心率为"+HeartBpmList.get(i));
                Thread.sleep(1000);
                List<Double> doubleList = this.heartV7DataBuild("1111", HeartBpmList.get(i));
                System.out.println("第"+i+"个数的数组为"+doubleList);
                saveDate.addAll(doubleList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        System.out.println("最终输出数据为："+saveDate);
        System.out.println("原始数据为：0,0,0,-2,-1,2,5,9,5,2,-4,-3,0,0.25,0.25,0.25,0.5,1,1.5,2,2.5,2.75,3,3,3,2.75,2.5,2,1.5,1,0.5,0.5,0.5,0.25,0.25,0.5,0.5,0.25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.25,0.75,1.25,1.5,1.5,1.25,0.75,0.25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,-1,2,5,9,5,2,-4,-3,0,0.25,0.25,0.25,0.5,1,1.5,2,2.5,2.75,3,3,3,2.75,2.5,2,1.5,1,0.5,0.5,0.5,0.25,0.25,0.5,0.5,0.25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.25,0.75,1.25,1.5,1.5,1.25,0.75,0.25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,-1,2,5,9,5,2,-4,-3,0,0.25,0.25,0.25,0.5,1,1.5,2,2.5,2.75,3,3,3,2.75,2.5,2,1.5,1,0.5,0.5,0.5,0.25,0.25,0.5,0.5,0.25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.25,0.75,1.25,1.5,1.5,1.25,0.75,0.25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,-1,2,5,9,5,2,-4,-3,0,0.25,0.25,0.25,0.5,1,1.5,2,2.5,2.75,3,3,3,2.75,2.5,2,1.5,1,0.5,0.5,0.5,0.25,0.25,0.5,0.5,0.25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.25,0.75,1.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75,3,3,3,2.75,2.5,2,1.5,1,0.5,0.5,0.5,0.25,0.25,0.5,0.5,0.25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.25,0.75,1.25,1.5,1.5,1.25,0.75,0.25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,-1\n");
    }


    public List<Double> heartV7DataBuild(String sn , Integer heartBpm) throws Exception {
        List<Double> Buff_Electrocardiogram_QRST = new ArrayList<>();
        for (int i = 0; i < Buff_Electrocardiogram_QRST_S.length; i++) {
            Buff_Electrocardiogram_QRST.add(Double.valueOf(Buff_Electrocardiogram_QRST_S[i]));
        }
        List<Double> Buff_Electrocardiogram_PR = new ArrayList<>();
        for (int i = 0; i < Buff_Electrocardiogram_PR_S.length; i++) {
            Buff_Electrocardiogram_PR.add(Double.valueOf(Buff_Electrocardiogram_PR_S[i]));
        }
        //QRSTPR波对应的时间（毫秒）
        int HeartWave_QRSTPR_cnt = Buff_Electrocardiogram_QRST.size() + Buff_Electrocardiogram_PR.size();
        //心跳波形模型初始化
        List<Double> HeartWave_Y_Mode = new ArrayList<>(200);
        for (int i = 0; i < 200; i++) {
            HeartWave_Y_Mode.add(0.0);
        }
        return heartV7DataBuild(sn, heartBpm, HeartWave_QRSTPR_cnt, HeartWave_Y_Mode, Buff_Electrocardiogram_QRST, Buff_Electrocardiogram_PR);

    }




    //心跳数据构建
    public List<Double> heartV7DataBuild(String sn, Integer heartBpm, Integer HeartWave_QRSTPR_cnt, List<Double> HeartWave_Y_Mode,
                                         List<Double> Buff_Electrocardiogram_QRST, List<Double> Buff_Electrocardiogram_PR) throws Exception {
        User currentUser = RecoupUtil.getCurrentUser();
        String userIdAndSn = sn + ":" + currentUser.getId();
        RealTimeV7Cache realTimeV7Cache = new RealTimeV7Cache();
        //获取当前请求的缓存
        if (redisService.exists(WEB_HEART_V7_REAL_TIME_CACHE + userIdAndSn)) {
            String realTimeV7CacheString = redisService.get(WEB_HEART_V7_REAL_TIME_CACHE + userIdAndSn);
            realTimeV7Cache = JSON.parseObject(realTimeV7CacheString, RealTimeV7Cache.class);
        }else {
            redisService.set(WEB_HEART_V7_REAL_TIME_CACHE + userIdAndSn, JSON.toJSONString(realTimeV7Cache));
        }
        double T_cnt_present = realTimeV7Cache.getT_cnt_present();
        //当前从心跳模型中打点的位置
        int pointer = realTimeV7Cache.getPointer();
        if(realTimeV7Cache.getHeartWave_Y_Mode().size()!=0){
            HeartWave_Y_Mode = realTimeV7Cache.getHeartWave_Y_Mode();
        }
        if (heartBpm > HeartBPM_Max) {
            heartBpm = HeartBPM_Max;
        }
        if (heartBpm < HeartBPM_min) {
            heartBpm = HeartBPM_min;
        }
//        double RR = (60 / Double.valueOf(heartBpm)) * 1000 + (Math.random() - 0.5) * 600;
        double RR = (60 / Double.valueOf(heartBpm)) * 1000;
        // % 接收到的心跳周期的总点数
        long T_cnt_new = Math.round((RR / 1000) * HeartWaveFreq);
        //补点数
        long HeartWave_TP_cnt = T_cnt_new - HeartWave_QRSTPR_cnt;
        double HeartWave_Y = 0.0;
        List<Double> HeartWave_Y_List = new ArrayList<>();
        for (int j = 1; j <= HeartWaveFreq; j++) {
            pointer = pointer + 1;
            HeartWave_Y = HeartWave_Y_Mode.get(pointer);
            if (pointer >= T_cnt_present) {
                for (int k = 1; k < HeartWave_Y_Mode.size(); k++) {
                    if (k < Buff_Electrocardiogram_QRST.size()) {
                        for (int l = 0; l < Buff_Electrocardiogram_QRST.size(); l++) {
                            //数据固定
                            if (k == l) {
                                HeartWave_Y_Mode.set(k+1, Buff_Electrocardiogram_QRST.get(l));
                                break;
                            }
                        }
                    }
                    if (k >= Buff_Electrocardiogram_QRST.size()) {
                        for (int l = Buff_Electrocardiogram_QRST.size(); l < Math.toIntExact(HeartWave_TP_cnt) + Buff_Electrocardiogram_QRST.size(); l++) {
                            //补0
                            if (k == l) {
                                HeartWave_Y_Mode.set(k+1, 0.0);
                                break;
                            }
                        }
                    }
                    //数据写死
                    int i1 = Math.toIntExact(HeartWave_TP_cnt) + Buff_Electrocardiogram_QRST.size();
                    if (k >= i1) {
                        int i2 = Math.toIntExact(HeartWave_TP_cnt) + Buff_Electrocardiogram_QRST.size() + Buff_Electrocardiogram_PR.size();
                        for (int l = i1; l < i2; l++) {
                            for (int m = 0; m < Buff_Electrocardiogram_PR.size(); m++) {
                                if (k == l && l - i1 == m) {
                                    HeartWave_Y_Mode.set(k+1, Buff_Electrocardiogram_PR.get(m));
                                    break;
                                }
                            }
                        }
                    }
                }
                //更新当前心跳周期总点数
                T_cnt_present = Buff_Electrocardiogram_QRST.size() + HeartWave_TP_cnt + Buff_Electrocardiogram_PR.size();
                pointer = 0;
            }
            HeartWave_Y_List.add(HeartWave_Y);
        }
        realTimeV7Cache.setPointer(pointer);
        realTimeV7Cache.setT_cnt_present(T_cnt_present);
        realTimeV7Cache.setHeartWave_Y_Mode(HeartWave_Y_Mode);
        redisService.set(WEB_HEART_V7_REAL_TIME_CACHE + userIdAndSn, JSON.toJSONString(realTimeV7Cache));
        return HeartWave_Y_List;
    }


    //呼吸数据构建
    public List<Double> breathV7DataBuild(String sn, Integer breathBpm, Double breathDeepth) throws Exception {
        double Phi = 0.0;
        User currentUser = RecoupUtil.getCurrentUser();
        String userIdAndSn = sn + ":" + currentUser.getId();
        String WEB_BREATH_OPERATION_V7_SN = WEB_BREATH_OPERATION_V7 + userIdAndSn;
        String WEB_BREATH_PHI_V7_SN = WEB_BREATH_PHI_V7 + userIdAndSn;
        while (redisService.lsize(WEB_BREATH_OPERATION_V7_SN) < 5) {
            redisService.lpush(WEB_BREATH_OPERATION_V7_SN, "0");
        }
        if (redisService.exists(WEB_BREATH_PHI_V7_SN)) {
            Phi = Double.parseDouble(redisService.get(WEB_BREATH_PHI_V7_SN));
        }
        double BreathWave_A;
        //实际业务
        //如果呼吸深度大于呼吸深度的阈值
        if (breathDeepth > BreathDeepth_th) {
            //当前帧的呼吸幅值
            BreathWave_A = 5.0;
        } else {
            BreathWave_A = 2.0;
        }
        //当前帧的呼吸BPM，对应变量breathRateBpmEstOutput
        double BreathWave_f = Double.valueOf(breathBpm) / 60;
        if (breathBpm == 0) {
            //当前帧的呼吸BPM，发生屏息时，调整波形
            BreathWave_f = 0.25;
            BreathWave_A = 0.1;
        }
        double BreathWave_Y = 0.0;
        List<Double> breathBpmList = new ArrayList<>();
        for (int j = 1; j <= 10; j++) {
            double BreathWave_Ytemp = BreathWave_A * Math.cos((2 * PI * BreathWave_f) / 10 * j + Phi);
            redisService.rpop(WEB_BREATH_OPERATION_V7_SN);
            redisService.lpush(WEB_BREATH_OPERATION_V7_SN, String.valueOf(BreathWave_Ytemp));
            //获取前五个
            List<String> lrange = redisService.lrange(WEB_BREATH_OPERATION_V7_SN, 0, 4);
            double sum = 0.0;
            for (int k = 0; k < lrange.size(); k++) {
                sum += Double.parseDouble(lrange.get(k));
            }
            BreathWave_Y = sum / 5;
            System.out.println(BreathWave_Y);
            breathBpmList.add(BreathWave_Y);
        }
        Phi = (Phi + (2 * PI * BreathWave_f) / 10 * 11) % (2 * PI);
        redisService.set(WEB_BREATH_PHI_V7_SN, String.valueOf(Phi));
        return breathBpmList;
    }


    //构建呼吸BPM
    private List<Integer> buildBreathBpm() {
        List<Integer> breathBpmList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            if (i < 10) {
                breathBpmList.add(10);
            }
            if (10 <= i && i < 20) {
                breathBpmList.add(20);
            }
            if (20 <= i && i < 30) {
                breathBpmList.add(30);
            }
            if (30 <= i && i < 40) {
                breathBpmList.add(0);
            }
            if (40 <= i) {
                breathBpmList.add(20);
            }
        }
        return breathBpmList;
    }

    //构建呼吸深度
    private List<Double> buildBreathDeepth() {
        List<Double> breathDeepthList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            if (i < 10) {
                breathDeepthList.add(10.0);
            }
            if (10 <= i && i < 20) {
                breathDeepthList.add(5.0);
            }
            if (20 <= i && i < 30) {
                breathDeepthList.add(0.5);
            }
            if (30 <= i && i < 40) {
                breathDeepthList.add(20.0);
            }
            if (40 <= i) {
                breathDeepthList.add(20.0);
            }
        }
        return breathDeepthList;
    }

    //构建心跳BPM
    private static List<Integer> buildHeartBpm() {
        List<Integer> heartBpmList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            if (i < 10) {
                heartBpmList.add(65);
            }
            if (10 <= i && i < 20) {
                heartBpmList.add(80);
            }
            if (20 <= i && i < 30) {
                heartBpmList.add(70);
            }
            if (30 <= i && i < 40) {
                heartBpmList.add(65);
            }
            if (40 <= i) {
                heartBpmList.add(80);
            }
        }
        return heartBpmList;
    }

}
